package Extend;
use strict;

sub import {
	my ($pkg) = @_;
	no strict 'refs';
	my $extended = caller;
	${$extended."::package"} = $extended;
	*{$extended."::import"} = sub  {
		my($rpkg,@isa) = @_;
		my $caller = caller;
		no strict 'refs';
		if(@isa){
			my @risa;
			for my $base(@isa){
				_soft_require($base);
				push @risa,$base;
			}
			push @{$rpkg."::ISA"}, @risa;
			${$extended."::package"} = $rpkg;
		}
		my $exp = ${$extended."::"}{EXPORT};
		if($exp && *$exp{ARRAY}){
			for my $sub (@{$extended."::EXPORT"}){
				*{$caller."::".$sub} = \&{$extended."::".$sub};
			}
		}
	};
	*{$extended."::Class"} = sub {
		return ${$extended."::package"};
	};
}
sub _soft_require {
	my $package = shift;
	no strict 'refs';
	my $error;
	{
		local $SIG{__DIE__};
		local $@;
		eval "require $package;";
		$error = $@;
	}
	die $error if $error and $error !~ /^Can't locate .*? at \(eval/;
	unless (%{$package."::"}) {
		die "Cannot found package '$package'";
	}
}
sub soft_require {
	my $package = shift;
	unless (ref $package){
		$package = [$package];
	}
	for(@$package){
		_soft_require($_);
	}
}
1;
